<?php
namespace App\Http\Controllers\Admin\Job;

use App\Repositories\Admin\JobRepository;
use App\Utilities\JiugeForm;
use Illuminate\Http\Request;

class JobController extends BaseController
{
    /**
     * 岗位
     */

    protected $view = 'admin.job.job.';//视图前缀
    protected $url = 'job';//路由前缀
    protected $model;
    protected $formElementArr = [//表单元素
        //array(表单选项,中文名称,name字段名称,表单提示,js验证规则)
        array(1,'岗位名称','name','岗位名称，2-20字符',1),
        array(5,'所属父级','pid','父级分类',6),
    ];
    protected $fieldShowArr = [
        'id' => 'ID',
        'name' => '岗位名称',
        'pid' => '所属父级',
        'createTime' => '创建时间',
    ];

    public function __construct()
    {
        parent::__construct();
        $this->prefix_url = $this->prefix_url.'/'.$this->url;
        $this->model = new JobRepository();
        view()->share('prefix_url',$this->prefix_url);
        view()->share('jiugeForm',new JiugeForm($this->prefix_url));
    }

    public function index(Request $request)
    {
        $type = isset($request->type) ? $request->type : 1;
        $models = $this->model->getIndexList($type);
        return view($this->view.'index',array(
            'datas' => $models,
        ));
    }

    /**
     * 通过员工类型，获取岗位列表
     */
    public function getJobsByStaffType(Request $request)
    {
        if ($request->ajax()) {
            $staffType = $request->staffType;
            $resJobs = $this->model->getListByStaffType($staffType);
            if (!$resJobs) {
                return array('code'=>-2,'msg'=>'没有数据！');
            }
            return array('code'=>0,'msg'=>'操作成功！','data'=>$resJobs);
        }
        return array('code'=>-1,'msg'=>'操作失败！');
    }

    public function create()
    {
        return view($this->formView.'edit',array(
            'sels' => $this->formElementArr,
            'options' => array(
                'pid' => $this->model->getParents(),
            ),
        ));
    }

    public function store(Request $request)
    {
        $data = $this->getData($request);
        $this->model->add($data);
        return redirect($this->prefix_url);
    }

    public function edit($id)
    {
        return view($this->formView.'edit',array(
            'data' => $this->getModel($id),
            'sels' => $this->formElementArr,
            'options' => array(
                'pid' => $this->model->getParents(),
            ),
        ));
    }

    public function update(Request $request,$id)
    {
        $model = $this->getModel($id);
        $data = $this->getData($request);
        $this->model->modify($data,$id);
        return redirect($this->prefix_url);
    }

    public function getData(Request $request)
    {
        if (!$request->name || !$request->cate) {
            echo "<script>alert('参数不全！');history.go(-1);</script>"; exit;
        }
        return array(
            'name' => $request->name,
            'cate' => $request->cate,
            'pid' => $request->pid,
        );
    }

    public function getModelById($id)
    {
        $model = $this->model->getShowOne($id);
        if (!$model) {
            echo "<script>alert('记录不存在！');history.go(-1);</script>"; exit;
        }
        return $model;
    }

    /**
     * 清空用户数据
     */
    public function setClean()
    {
        $resModel = $this->model->setClean();
        if ($resModel!=0) {
            echo "<script>";
            echo "alert('操作失败！');";
            echo "history.go(-1);";
            echo "</script>";
            exit;
        }
        return redirect($this->prefix_url);
    }
}
